﻿using CompanyManagementDLL.MetaData;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CompanyManagementDLL.DLL
{
    public class TaskDLL
    {
        private readonly CompanyMgtEntities db = new CompanyMgtEntities();
        #region Fecth Data
        /// <summary>
        /// Return task detail based in task id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public TaskMetaData GetTaskByID(int id)
        {
            TaskMetaData taskmetadata = new TaskMetaData();
            try
            {
                taskmetadata = db.Tasks.Where(r => r.TaskID == id).Select(u => new TaskMetaData
                {
                    TaskName = u.TaskName,
                    EstimatedHours = u.EstimatedHours??0,
                    ModuleID = u.ModuleID,
                    TaskID = u.TaskID,
                    CreatedBy = u.CreatedBy,
                    CreatedDate = u.CreatedDate,
                    IsActive = u.IsActive ?? false,
                    IsArchive = u.IsArchive,

                }).FirstOrDefault();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return taskmetadata;
        }

        #endregion

        #region Get List

        /// <summary>
        /// Retrun task list
        /// </summary>
        /// <returns></returns>
        public List<TaskMetaData> GetTaskList()
        {
            List<TaskMetaData> TaskMetaData = new List<TaskMetaData>();
            try
            {
                TaskMetaData = db.Tasks.Where(u => u.IsActive == true && u.IsArchive == false).Select(u => new TaskMetaData
                {
                    TaskName=u.TaskName,
                    EstimatedHours=u.EstimatedHours??0,
                    ModuleID=u.ModuleID,
                    TaskID=u.TaskID,
                    CreatedBy=u.CreatedBy,
                    CreatedDate = u.CreatedDate,
                    IsActive = u.IsActive ?? false,
                    IsArchive = u.IsArchive
                }).ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return TaskMetaData;
        }

        #endregion

        #region DML Opration

        /// <summary>
        /// insert task entity
        /// </summary>
        /// <param name="TaskMetaData"></param>
        public void InsertTask(TaskMetaData TaskMetaData)
        {
            Task tblTask = new Task();
            try
            {
                tblTask.TaskName = TaskMetaData.TaskName;
                tblTask.ModuleID = TaskMetaData.ModuleID;
                tblTask.IsActive = TaskMetaData.IsActive;
                tblTask.IsArchive = false;
                tblTask.EstimatedHours = TaskMetaData.EstimatedHours;
                tblTask.CreatedBy = TaskMetaData.CreatedBy;
                tblTask.CreatedDate = DateTime.Now;
                db.Tasks.Add(tblTask);
                db.SaveChanges();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        /// <summary>
        /// update task entity
        /// </summary>
        /// <param name="TaskMetaData"></param>
        public void UpdateTask(TaskMetaData TaskMetaData)
        {
            Task tblTask = new Task();
            try
            {
                tblTask.TaskName = TaskMetaData.TaskName;
                tblTask.ModuleID = TaskMetaData.ModuleID;
                tblTask.IsActive = TaskMetaData.IsActive;
                tblTask.EstimatedHours = TaskMetaData.EstimatedHours;
                tblTask.IsArchive = TaskMetaData.IsArchive == true ? TaskMetaData.IsArchive : false;
                tblTask.IsActive = TaskMetaData.IsActive;
                db.SaveChanges();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        /// <summary>
        /// delete task entity
        /// </summary>
        /// <param name="id"></param>
        public void DeleteTask(int id)
        {
            try
            {
                Task tblTask = new Task();
                tblTask = db.Tasks.Where(r => r.TaskID == id).FirstOrDefault();
                tblTask.IsArchive = true;
                db.SaveChanges();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        #endregion
    }
}
